package com.itcast.cn;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

import android.os.Bundle;
import android.os.RemoteException;
import android.provider.Contacts.Intents.UI;

public class TwoDemo extends UiAutomatorTestCase{
	public static void main(String[] args) {
		String jarName, testClass, testName, androidId;
		jarName = "dmeo1";
		testClass = "com.itcast.cn.TwoDemo";
		testName = "testUiScrollable";
		androidId = "14";
		new UiAutomatorHelper(jarName, testClass, testName, androidId);
	}
	
	public void testDragAndSwipe() {
		//[110,709][194,851]
		int startX, startY, endX, endY, steps;
		startX = (194-110)/2+110;
		startY = (851-709)/2+709;
		endX = startX;
		endY = startY-200;
		steps = 10;
		UiDevice.getInstance().drag(startX, startY, endX, endY, steps);
	}
	
	public void testClick() throws RemoteException, UiObjectNotFoundException {
		UiDevice.getInstance().click(181, 306);
		UiObject view = new UiObject(new UiSelector().className("android.view.View"));
		view.click();
		view.click();
		UiObject textView = new UiObject(new UiSelector().className("android.widget.TextView"));
		textView.click();
		UiObject button = new UiObject(new UiSelector().className("android.widget.Button"));
		button.click();
		sleep(5000);
		if(UiDevice.getInstance().isNaturalOrientation()) {
			UiDevice.getInstance().setOrientationLeft();
		}
	}
	
	public void testOpenNotification() {
		UiDevice.getInstance().openNotification();
		sleep(2000);
		UiDevice.getInstance().openQuickSettings();
	}
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		Bundle bundle = new Bundle();
		bundle.putString("key1", "value1");
		bundle.putString("key2", "value2");
		bundle.putString("key3", "value3");
		bundle.putString("key4", "value4");
		bundle.putString("key5", "value5");
		getAutomationSupport().sendStatus(10, bundle);
	}
	
	public void testReport() {
		Bundle bundle = new Bundle();
		bundle.putString("key1*", "value1*");
		bundle.putString("key2*", "value2*");
		bundle.putString("key3*", "value3*");
		bundle.putString("key4*", "value4*");
		bundle.putString("key5*", "value5*");
		getAutomationSupport().sendStatus(11, bundle);
		UiDevice.getInstance().pressMenu();
	}
	
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		Bundle bundle = new Bundle();
		bundle.putString("key1-", "value1-");
		bundle.putString("key2-", "value2-");
		bundle.putString("key3-", "value3-");
		bundle.putString("key4-", "value4-");
		bundle.putString("key5-", "value5-");
		getAutomationSupport().sendStatus(12, bundle);
	}
	
	public void testCall() throws UiObjectNotFoundException {
		UiObject phoneObject = new UiObject(new UiSelector().text("Phone"));
		UiObject dialPad = new UiObject(new UiSelector().description("dial pad"));
		phoneObject.clickAndWaitForNewWindow();
		dialPad.clickAndWaitForNewWindow();
		
		Bundle bundle = getParams();
		String phone = (String) bundle.get("phone");
		
		for(int i=0; i<phone.length(); i++) {
			String c = phone.charAt(i)+"";
			UiObject phoneNum = new UiObject(new UiSelector().text(c));
			phoneNum.click();
			sleep(2000);
		}
	}
	
	public void testUiScrollable() throws UiObjectNotFoundException {
		UiObject phoneObject = new UiObject(new UiSelector().text("Phone"));
		phoneObject.clickAndWaitForNewWindow();
		UiObject contactsObject = new UiObject(new UiSelector().description("Contacts"));
		contactsObject.clickAndWaitForNewWindow();
		UiScrollable listContacts = new UiScrollable(new UiSelector().resourceId("android:id/list"));
		listContacts.flingToEnd(60);
	}
}
